//
//  JHSCommonWealController.m
//  JHSSweep
//
//  Created by 卢春晖 on 2017/3/26.
//  Copyright © 2017年 JHDS. All rights reserved.
//

#import "JHSCommonWealController.h"
#import <sys/socket.h>
#import <netinet/in.h>
#import <arpa/inet.h>

@interface JHSCommonWealController () {
    int socketid;
}

@property (nonatomic, strong) UIWebView *webView;

@end

@implementation JHSCommonWealController

- (void)viewDidLoad {
    [super viewDidLoad];
    _webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_webView];
    [self loadBaidu];
}

- (void) loadBaidu {
    NSString *urlString = @"http://baike.baidu.com/link?url=RxoAJ1I8BxDT-MLADE1MyPSrTDQ-ZBxQWckghcA-HsiglgDh0DJhz4RGIzMTaa5x2k8fMLQDev5WpSL0sh6mpUZTmdXtg7kRDwXithBNHTG";
//    NSString *urlString = @"https://www.baidu.com";

    //创建一个url的对象
    NSURL *url = [NSURL URLWithString:urlString];
    //创建一个reqeust对象
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    /*
     NSURLConnection: 在ios9.0之后已经被废弃
     sendAsynchronousRequest: 发起一个异步的请求
     queue: 请求完成之后, 需要做的相当操作所在的线程, 一般都是更新UI,所有一般是主线程
     completionHandler: 网络请求完之后要做的事, 回调
     
     completionHandler的相关参数:
     response: 响应的对象
     data: 响应的数据, 真正需要展示的数据
     connectionError: 如果请求失败的错误信息
     */
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        NSLog(@"%@", response);
        //创建html字符串
        NSString *htmlString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@", htmlString);
        
        [_webView loadHTMLString:htmlString baseURL:url];
    }];
}


/**
 使用NSURLSession加载百度页面
 */
- (void) loadBaiduTwo {
    NSString *urlString = @"https://www.baidu.com";
    //创建一个url的对象
    NSURL *url = [NSURL URLWithString:urlString];
    //创建一个reqeust对象
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    //使用NSURLSession的单例, 创建一个NSURLSessionDataTask的对象
    // task 默认是挂起状态, 需要手动 resume
    NSURLSessionDataTask * task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"%@", response);
        //创建html字符串
        NSString *htmlString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        NSLog(@"%@", htmlString);
        
        [_webView loadHTMLString:htmlString baseURL:url];
    }];
    
    [task resume];
}




@end
